//
//  UIFactory+Extension.swift
//  WeiBo
//
//  Created by Eric on 2017-01-06.
//  Copyright © 2017 Eric. All rights reserved.
//

import UIKit

//扩展UILabel的构造方法
extension UILabel {
    convenience init(text: String, fonSize: CGFloat, textColor: UIColor) {
        self.init()
        //设置属性
        self.text = text
        self.font = UIFont.systemFont(ofSize: fonSize)
        self.numberOfLines = 0
        self.textColor = textColor
        self.sizeToFit()
    }
}

//扩展UIButton构造方法
extension UIButton {
    convenience init(backgroundImageName: String, title: String, fontSize: CGFloat, textColor: UIColor) {
        self.init()
        //设置属性
        setBackgroundImage(UIImage(named: backgroundImageName), for: .normal)
        self.setTitle(title, for: .normal)
        self.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        self.setTitleColor(textColor, for: .normal)
        self.sizeToFit()
    }
}


//扩展UIBarButtonItem的构造方法
extension UIBarButtonItem {
    
    convenience init(imageName: String? = nil, titleName: String?, target: AnyObject?, action: Selector?) {
        
        let btn = UIButton()
        
        if let image = imageName {
            btn.setImage(UIImage(named: image), for: .normal)
            btn.setImage(UIImage(named: "\(image)_highlighted"), for: .highlighted)
        }
        
        if let title = titleName {
            btn.setTitle(title, for: .normal)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            btn.setTitleColor(UIColor.gray, for: .normal)
            btn.setTitleColor(UIColor.orange, for: .highlighted)
        }
        
        btn.sizeToFit()
        
        //点击事件
        if let act = action {
            btn.addTarget(target, action: act, for: .touchUpInside)
        }
        
        self.init()
        
        self.customView = btn
    }
    
}
